package container

import (
	"os"

	"chainmaker.org/chainmaker/smarteditor/conf"
	"chainmaker.org/chainmaker/smarteditor/limit"
	"chainmaker.org/chainmaker/smarteditor/logger"
	"chainmaker.org/chainmaker/smarteditor/pool"
	"chainmaker.org/chainmaker/smarteditor/server"
	"chainmaker.org/chainmaker/smarteditor/util"
	"go.uber.org/zap"
)

//SC is a container to reference system object
var SC *serverContainer

type serverContainer struct {
	SystemResourcePool *pool.SystemResourcePool
	ResourceManager    *limit.UserResourcesManager
	SessionManager     *SessionManager
	//CookieStore        *sessions.CookieStore
	Logger             *zap.SugaredLogger
	RpcServerAddress   string
	RpcServer          *server.ContractRpcServer
	BuildContractCache *BuildContractCache
}

func InitServerContainer() {
	SC = &serverContainer{
		SystemResourcePool: pool.NewSystemResourcePool(conf.Wide.ResourcePoolSize),

		ResourceManager: limit.NewUserResourcesManager(conf.Wide.MaxContractCount, conf.Wide.MaxFileCount),
		SessionManager:  NewSessionManager(),
		//CookieStore:     sessions.NewCookieStore([]byte("BEYOND")),
		Logger: logger.Get(),

		BuildContractCache: new(BuildContractCache),
	}

	ip, err := util.ExternalIP()
	if err != nil {
		SC.Logger.Errorf("[InitServerContainer] get external ip fail. err:%s", err)
		os.Exit(-1)
		return
	}

	SC.RpcServerAddress = ip.String() + conf.Wide.GrpcPort
}

func InitEmptyServerContainer() {
	SC = &serverContainer{}
}
